/*
 * Implementation of ProgressItem.
 *
 * Encapsulates the user's progress with learning a Phrase.
 */

#include <QtGui>

#include "includes/progressitem.h"

// default constructor
ProgressItem::ProgressItem()
{
    // record current date
    date = QDate::currentDate();
}

// date
const QDate& ProgressItem::getDate() const {return date;}

// duration
void ProgressItem::setDuration(const long& d) {duration = d;}
const long& ProgressItem::getDuration() const {return duration;}

// lesson name
void ProgressItem::setLessonName(const QString& name) {lessonName = name;}
const QString& ProgressItem::getLessonName() const {return lessonName;}

// phrase name
void ProgressItem::setPhraseName(const QString& name) {phraseName = name;}
const QString& ProgressItem::getPhraseName() const {return phraseName;}

// video file
void ProgressItem::setVideoFilename(const QString& f, const QString& user) {
    QFileInfo info(f);
    videoFilename = QString("Progress_Report/videos/")
                    + user + QString(" - ")
                    + this->lessonName + QString(" - ")
                    + this->phraseName + QString(".")
                    + info.completeSuffix();
    QFile::remove(videoFilename);
    QFile::copy(f, videoFilename);
}
const QString& ProgressItem::getVideoFilename() const {return videoFilename;}

// status
void ProgressItem::setStatus(const ProgressItem::ProgressStatus s) {status = s;}
ProgressItem::ProgressStatus ProgressItem::getStatus() {return status;}

bool ProgressItem::operator ==(const ProgressItem& other)
{
    return (this->lessonName == other.getLessonName()
            && this->phraseName == other.getPhraseName());
}

// save as dom node
QDomElement& ProgressItem::toDomElement (QDomElement& node) {
        // document
        QDomDocument doc = node.ownerDocument();

        // parent
        node.setTagName("progress_item");
        // date & duration
        node.setAttribute("date", date.toString("dd/MM/yyyy"));
        node.setAttribute("duration", QString::number(duration));
        // lesson name
        QDomElement lessonNameElement = doc.createElement("lesson");
        lessonNameElement.appendChild(doc.createTextNode(lessonName));
        node.appendChild(lessonNameElement);
        // phrase name
        QDomElement phraseNameElement = doc.createElement("phrase");
        phraseNameElement.appendChild(doc.createTextNode(phraseName));
        node.appendChild(phraseNameElement);
        // video filename
        QDomElement videoFileNameElement = doc.createElement("video");
        videoFileNameElement.appendChild(doc.createTextNode(videoFilename));
        //qDebug() << "In ProgressItem::toDomElement with Vid = " << videoFilename;
        node.appendChild(videoFileNameElement);
        // progress status
        QDomElement statusElement = doc.createElement("status");
        statusElement.appendChild(doc.createTextNode(QString::number(status)));
        node.appendChild(statusElement);

        // return reference to same node
        return node;
}

// load from DOM node
void ProgressItem::fromDomElement(QDomElement& node) {
        if ("progress_item" != node.tagName())
                return;

        // date of creation
        // regex used to reconstruct QDate object from stored string
        QRegExp dateExp("^(\\d\\d)/(\\d\\d)/(\\d\\d\\d\\d)$");
        QString dateString = node.attribute("date");
        // process string
        int pos = dateExp.indexIn(dateString);
        if (pos > -1) {
                date.setDate(dateExp.cap(3).toInt(), dateExp.cap(2).toInt(), dateExp.cap(1).toInt());
        }

        // duration
        duration = node.attribute("duration").toLong();
        // lesson name
        lessonName = node.elementsByTagName("lesson").item(0).firstChild().nodeValue();
        // phrase name
        phraseName = node.elementsByTagName("phrase").item(0).firstChild().nodeValue();
        // video filename
        videoFilename = node.elementsByTagName("video").item(0).firstChild().nodeValue();

        //qDebug() << "In ProgressItem::fromDomElement with video = " << videoFilename;

        // status
        status = (ProgressItem::ProgressStatus)node.elementsByTagName("status").item(0).firstChild().nodeValue().toInt();
}
